package cn.com.controller.frontController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Store;
import cn.com.db.service.StoreService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Auth(UserAuth.USER)
@RestController
@RequestMapping("/front/store")
public class StoreController {

    @Autowired
    StoreService storeService;

    @RequestMapping(path = "/{id}",method = RequestMethod.GET)
    public String viewStore(HttpServletRequest request,
                              @PathVariable("id")String id)
    {
        DataResult dataResult=new DataResult();
        Store store=storeService.getStoreById(id);
        storeService.modifyEchoTime(store);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        dataResult.setData(store);
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(path = "/batch",method = RequestMethod.POST)
    public String batchGetStore(HttpServletRequest request,
                           @RequestParam("storeIds") String  storeIds)
    {
        DataResult dataResult=new DataResult();
        List<String> storeIdList= UidUtil.splitStringByComma(storeIds);
        if (storeIdList==null||storeIdList.size()==0){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("批量获取店铺信息的参数不合法");
            return JsonUtil.toJsonString(dataResult);
        }
        List<Store> stores = storeService.batchGetSotre(storeIdList);
        storeService.modifyEchoTime(stores);
        dataResult.setData(stores);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setTotal(stores.size());
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(path = "/search",method = RequestMethod.POST)
    public String getStore(HttpServletRequest request,
                           Store store,
                           @RequestParam("page") Integer page,
                           @RequestParam("count") Integer count)
    {
        DataResult dataResult=new DataResult(page,count);
        List<Store> list=storeService.searchStore(store, page, count);
        Integer total=storeService.countStore(store);
        storeService.modifyEchoTime(list);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        dataResult.setData(list);
        dataResult.setTotal(total);
        return JsonUtil.toJsonString(dataResult);
    }
}
